package com.guoshengdeu.door.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoshengdeu.door.entity.SysManager;
import com.guoshengdeu.door.service.ISysManagerService;
import com.guoshengdeu.door.util.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yfly
 * @since 2021-06-01
 */
@Api("账号管理")
@RestController
@RequestMapping("/sys-manager")
public class SysManagerController {

    @Autowired
    private ISysManagerService sysManagerService;

    //管理员登录
    @ApiOperation("管理员登录")
    @PostMapping("login")
    public RespResult login(@RequestBody SysManager sysManager, HttpServletRequest request){
        SysManager one = sysManagerService.getOne(new QueryWrapper<>(sysManager));
        if (one != null && 1 == one.getStatus()){
            request.getSession().setAttribute("sys_manager",one);
            return RespResult.success(one);
        }
        return RespResult.error("用户名或密码错误,或为封禁状态");
    }

    //重置密码
    @ApiOperation("密码重置")
    @RequestMapping("reset-pwd")
    public RespResult resetPassword(@RequestBody SysManager sysManager) {
        boolean saveOrUpdate = sysManagerService.saveOrUpdate(sysManager);
        if (saveOrUpdate){
            return RespResult.success();
        }
        return RespResult.error("密码重置失败");
    }

    // 查询
    @ApiOperation("查询")
    @RequestMapping("list")
    public List<SysManager> querySys(){
        return sysManagerService.list();
    }

    //添加
    @ApiOperation("添加")
    @RequestMapping("addsys")
    public RespResult addSys(@RequestBody SysManager sysManager){
        //先获取前端输入的账号
        String managerNum = sysManager.getManagerNum();
        //获取数据库中的账号
        SysManager sm = new SysManager();
        sm.setManagerNum(managerNum);
//        sm.setStatus(1);
        //与数据库中账号进行比较，看是否有相同账号(不能出现有2个及以上账号相同的情况)
        SysManager one = sysManagerService.getOne(new QueryWrapper<>(sm));
        if(null == one) {
            //添加账号
            boolean save = sysManagerService.save(sysManager);
            if (save){
                return RespResult.success();
            }
        }
        return  RespResult.error("添加失败,账号已存在");
    }

    //更新
    @ApiOperation("更新")
    @PutMapping("updatesys")
    public RespResult updatesys(@RequestBody SysManager sysManager){
        boolean updateById = sysManagerService.updateById(sysManager);
        if (updateById){
            return RespResult.success();
        }
        return RespResult.error("更新失败！");
    }

    //更新
    @ApiOperation("更新")
    @PutMapping("updatesys")
    public RespResult updatesys(@RequestBody SysManager sysManager){
        boolean updateById = sysManagerService.updateById(sysManager);
        if (updateById){
            return RespResult.success();
        }
        return RespResult.error("更新失败！");
    }

    //更新
    @ApiOperation("更新")
    @PutMapping("updatesys")
    public RespResult updatesys(@RequestBody SysManager sysManager){
        boolean updateById = sysManagerService.updateById(sysManager);
        if (updateById){
            return RespResult.success();
        }
        return RespResult.error("更新失败！");
    }

    //更新
    @ApiOperation("更新")
    @PutMapping("updatesys")
    public RespResult updatesys(@RequestBody SysManager sysManager){
        boolean updateById = sysManagerService.updateById(sysManager);
        if (updateById){
            return RespResult.success();
        }
        return RespResult.error("更新失败！");
    }

    //更新
    @ApiOperation("更新")
    @PutMapping("updatesys")
    public RespResult updatesys(@RequestBody SysManager sysManager){
        boolean updateById = sysManagerService.updateById(sysManager);
        if (updateById){
            return RespResult.success();
        }
        return RespResult.error("更新失败！");
    }

    //删除
    @ApiOperation("删除")
    @DeleteMapping("delesys")
    public RespResult deleSys(int id){
        boolean b = sysManagerService.removeById(id);
        if (b){
            return RespResult.success();
        }
        return RespResult.error("删除失败");
    }

    //分页
    @GetMapping("page")
    public RespResult page(Page<SysManager> page){
        Page<SysManager> pages = sysManagerService.page(page);
        return RespResult.success(pages);
    }

}
